//
//  NSMutableDictionary+AvoidCrash.m
//  LzlPictureBrower
//
//  Created by luozhilin on 2018/6/5.
//  Copyright © 2018年 luozhilin. All rights reserved.
//

#import "NSMutableDictionary+AvoidCrash.h"
#import "YYAvoidCrash.h"

@implementation NSMutableDictionary (AvoidCrash)

+ (void)avoidCrashExchangeMethod {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class dictionaryM = NSClassFromString(@"__NSDictionaryM");
        
        //setObject:forKey:
        [YYAvoidCrash exchangeInstanceMethod:dictionaryM originalMethod:@selector(setObject:forKey:) newMethod:@selector(yy_avoidCrashSetObject:forKey:)];
        
        //setObject:forKeyedSubscript:
        if (AvoidCrashVersion(11.0)) {
            [YYAvoidCrash exchangeInstanceMethod:dictionaryM originalMethod:@selector(setObject:forKeyedSubscript:) newMethod:@selector(yy_avoidCrashSetObject:forKeyedSubscript:)];
        }
        
        //removeObjectForKey:
        [YYAvoidCrash exchangeInstanceMethod:dictionaryM originalMethod:@selector(removeObjectForKey:) newMethod:@selector(yy_avoidCrashRemoveObjectForKey:)];
    });
}


//=================================================================
//                       setObject:forKey:
//=================================================================
#pragma mark - setObject:forKey:

- (void)yy_avoidCrashSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
    
    @try {
        [self yy_avoidCrashSetObject:anObject forKey:aKey];
    }
    @catch (NSException *exception) {
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
    }
    @finally {
        
    }
}

//=================================================================
//                  setObject:forKeyedSubscript:
//=================================================================
#pragma mark - setObject:forKeyedSubscript:
- (void)yy_avoidCrashSetObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
    @try {
        [self yy_avoidCrashSetObject:obj forKeyedSubscript:key];
    }
    @catch (NSException *exception) {
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
    }
    @finally {
        
    }
}


//=================================================================
//                       removeObjectForKey:
//=================================================================
#pragma mark - removeObjectForKey:

- (void)yy_avoidCrashRemoveObjectForKey:(id)aKey {
    
    @try {
        [self yy_avoidCrashRemoveObjectForKey:aKey];
    }
    @catch (NSException *exception) {
        [YYAvoidCrash errorMessageWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
    }
    @finally {
        
    }
}

@end
